
package com.x52im.mall;

import android.app.Activity;
import android.os.AsyncTask;

import com.eva.framework.dto.DataFromClient;
import com.eva.framework.dto.DataFromServer;

/**
 * 支付出错后处理本地存储的备用支付数据的后台线程.
 * <p>
 * 为了安全考虑，此线程向服务端提交数据时需要用户已经登陆，如果用户未登陆则将不
 * 向服务端提交数据.
 * 
 * @author Jack Jiang(http://www.52im.net/space-uid-1.html)
 * @version 1.0
 */
public abstract class CommonDataAsync extends AsyncTask<String, Integer, DataFromServer>
{
	private Activity activity = null;
	private String keyId;

	public CommonDataAsync(Activity activity)
	{
		this.activity = activity;
	}

	@Override
	protected DataFromServer doInBackground(String... params)
	{
		// 如果用户没有登陆则无条件不向服务端发数据（因为发数据是需要用户已经
		// 直到登陆后才发吧）
		if(!MallGlobal.getMallProviderInstance(activity).getRobotimeMall().isUserLogin())
			return Utility.constructFalseDFS();
		
		keyId = params[0];
		// dfc == null表示无需处理
		DataFromClient dfc = forDoInBackground(params[1], params.length >= 3 ? params[2] : "");
		if(dfc == null)
			return Utility.constructFalseDFS();
		
		return MallGlobal.getMallProviderInstance(activity).getRobotimeMall().getAuthedService().sendObjToServer(dfc);
	}

	protected void onPostExecute(DataFromServer dfs)
	{
		forOnPostExecute(dfs, keyId);
	}
	
	protected abstract DataFromClient forDoInBackground(String param, String clsName);

	protected abstract void forOnPostExecute(DataFromServer dfs, String keyId);
	

}
